﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Taurus.Platform.BLL;
using Taurus.Garden.BLL;
using Taurus.Platform.DTO;
using System.Threading;

namespace Taurus.Garden.UI
{
    public partial class GardenUI : UserControl
    {
        private AccountBll _account = new AccountBll();
        private Taurus.Components.Component _component;

        public GardenUI(Taurus.Components.Component component)
        {
            InitializeComponent();

            _component = component;
            dgvMyGarden.AutoGenerateColumns = false;
            dgvMySeed.AutoGenerateColumns = false;
            
        }

        private void GardenUI_Load(object sender, EventArgs e)
        {
            cbxAccount.DataSource = _account.GetUserAccount();
            cbxAccount.DisplayMember = "Name";
            cbxMoreOperation.SelectedIndex = 0;
        }

        private void cbxAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            //绑定我的农田与种子
            BindMyGarden();
            BindMySeed();
        }

        private void BindMyGarden()
        {
            var list = from vegetable in GetMyGarden().MyVegetable
                       orderby vegetable.IsShared , vegetable.Seed.Name
                       select new
                       {
                           Name = vegetable.Seed.Name,
                           vegetable.Water,
                           vegetable.Seed.ID,
                           vegetable.Percentage,
                           vegetable.FarmerName,
                           Shared = vegetable.IsShared ? "是" : "否",
                           vegetable.Count
                       };
            dgvMyGarden.DataSource = list.ToList();
        }

        private void BindMySeed()
        {
            dgvMySeed.DataSource = GetMyGarden().MySeeds;
        }


        private MyGarden GetMyGarden()
        {
            UserAccount ua = (UserAccount)cbxAccount.SelectedItem;
            return InitSys.Instance.GetMyGarden(ua.UID);
        }

        private void kryptonLabel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void cbxMoreOperation_SelectedIndexChanged(object sender, EventArgs e)
        {
            //更多操作
            MyGarden mg = GetMyGarden();
            mg.Remark = cbxMoreOperation.SelectedItem.ToString();
            switch (cbxMoreOperation.SelectedItem.ToString())
            {
                case "1.偷菜设置": 
                    break;
                case "2.收菜":
                    backgroundWorkerHarvest.RunWorkerAsync(mg);
                    break;
                case "3.种植植物":
                    break;
                case "4.偷菜":
                    backgroundWorkerHarvest.RunWorkerAsync(mg);
                    break;
                case "5.购买种子":
                    break;
                case "6.贩卖收割的植物":
                    break;
                case "7.犁地":
                    backgroundWorkerHarvest.RunWorkerAsync(mg);
                    break;
                default:
                    break;
            }
        }

        private void backgroundWorkerHarvest_DoWork(object sender, DoWorkEventArgs e)
        {
            MyGarden mg = (MyGarden) e.Argument;
            switch (mg.Remark)
            {
                case "1.偷菜设置":
                    break;
                case "2.收菜":
                    mg.Harvest();
                    break;
                case "3.种植植物":
                    break;
                case "4.偷菜":
                    mg.Steal();
                    break;
                case "5.购买种子":
                    break;
                case "6.贩卖收割的植物":
                    break;
                case "7.犁地":
                    mg.Plowing();
                    break;
                default:
                    break;
            }
            
        }

        private void backgroundWorkerHarvest_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _component.SendMessage(string.Empty);
            BindMyGarden();
        }
    }
}
